/**
 * 自定义用户
 * */

var formList = [];


//公司列表
function getIndex(){
    var formData = {
        start: pages.start,
        limit: pages.limit
    };
    $.ajax({
        type: apis.caseComIndex.method,
        url: apis.caseComIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            var d = res.data;
            $('#pageTotal').html(res.total);
            comHtml(d);
            var pageObj = pages; pageObj.total = res.total;
            pageHtml(pageObj);
        },
        error: function () {
            console.log('错误啦');
        }
    });
}
//公司字符串
function comHtml(d){
    var h = '', i;
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].cname + '</td>';
        h += '<td>';
        if (d[i].clogo == '') {
            h += '-';
        } else {
            h += '<img src="' + d[i].cprefix + d[i].clogo + '" height="50"/>';
        }
        h += '</td>';
        h += '<td>' + d[i].address + '</td>';
        h += '<td>' + d[i].uptime + '</td>';
        h += '<td class="f-14">';
        h += '<a href="javascript:;" onclick="lay_edit(\'编辑\',\'company-edit.html\', ' + d[i].id + ')">';
        h += '修改';
        h += '<i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        h += ' &nbsp; ';
        h += '<a href="javascript:;" onclick="lay_del(' + d[i].id + ');" style="color: red;">';
        h += '删除<i class="Hui-iconfont">&#xe6e2;</i>';
        h += '</a>';
        h += '<input type="hidden" id="cname-' + d[i].id + '" value="' + d[i].cname + '"/>';
        h += '<input type="hidden" id="clogo-' + d[i].id + '" value="' + d[i].clogo + '"/>';
        h += '<input type="hidden" id="address-' + d[i].id + '" value="' + d[i].address + '"/>';
        h += '</td>';
        h += '</tr>';
    }
    $('tbody').html(h);
}


//编辑弹窗
function lay_edit(title,url,id,w,h){
    url += '?id=' + id;
    var cname = $('#cname-' + id).val();
    var clogo = $('#clogo-' + id).val();
    var address = $('#address-' + id).val();
    cname = cname ? cname : '';
    clogo = clogo ? clogo : '';
    address = address ? address : '';
    url += '&cname=' + escape(cname);
    url += '&clogo=' + escape(clogo);
    url += '&address=' + escape(address);
    layer_show(title,url,w,h);
}


//获取详情
function getOne(){
    if (!id) { return }
    var h = '';
    $('#cname')[0].value = cname;
    $('#address')[0].value = address;
    if (clogo != '') {
        h = '';
        h += '<img src="/' + clogo + '"/>';
        h += '<input type="hidden" id="img" value="' + clogo + '"/>';
        $('#bigShow').html(h);
    }
}


//显示图片
function showImg(e){
    var h = '', i;
    h += '<img src="' + e.domain + e.img + '"/>';
    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
    $('#bigShow').html(h).height(e.h);
}


//保存
function modify(){
    var formData = {
        id: id,
        cname: $('#cname').val(),
        img: $('#img').val(),
        address: $('#address').val(),
    };
    if (!formData.cname) {
        msgPop({msg: '公司名称必须的'}); return
    }
    console.log(formData);
    //return
    $.ajax({
        type: apis.caseComModify.method,
        url: apis.caseComModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '更新成功'});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误辣');
        }
    });
}


//删除
function lay_del(id){
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg,function(index){
        $.ajax({
            type: apis.caseComDel.method,
            url: apis.caseComDel.url,
            data: { id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg}); return
                }
                layer.msg('已删除!',{icon: 1,time: 1000});
                setTimeout(function(){
                    top.location.reload();
                }, 1000);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}